﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 玩家可控制的物体的状态类
/// </summary>
[CreateAssetMenu(menuName = "MyScript/State/ControllableState/StateBase")]
public class ControllableObjectState : StateBase
{
    /// <summary>
    /// 是否需要出栈的标识
    /// </summary>
    [HideInInspector] public bool isNeedToPop = true;

    /// <summary>
    /// 是否在下推自动机顶的标识
    /// </summary>
    [HideInInspector] public bool isAtTop = false;

    /// <summary>
    /// 存储动作的容器
    /// </summary>
    [SerializeField] protected ControllableAct[] m_buttonDownActs;
    [SerializeField] protected ControllableAct[] m_buttonUpActs;
    [SerializeField] protected ControllableAct[] m_buttonPressedActs;

    protected ControllableObjectState m_tempState;

    protected ControllableObjectState m_returnState;

    /// <summary>
    /// 按钮按下的函数
    /// </summary>
    /// <param name="args"></param>
    public virtual ControllableObjectState ButtonDown(ButtonEventArgs args, GameObject gameObject)
    {
        m_returnState = ControllableStateMachine.remainState;

        for (int i = 0, length = m_buttonDownActs.Length; i < length; i++)
        {
            m_tempState = m_buttonDownActs[i].Act(args, gameObject, this);

            if (ControllableStateMachine.remainState != m_returnState && ControllableStateMachine.previousState != m_returnState)
            {
                continue;
            }

            if (ControllableStateMachine.remainState != m_tempState && m_tempState != m_returnState)
            {
                m_returnState = m_tempState;
            }
        }

        return m_returnState;
    }

    /// <summary>
    /// 按钮松开的函数
    /// </summary>
    /// <param name="args"></param>
    public virtual ControllableObjectState ButtonUp(ButtonEventArgs args, GameObject gameObject)
    {
        m_returnState = ControllableStateMachine.remainState;

        for (int i = 0, length = m_buttonUpActs.Length; i < length; i++)
        {
            m_tempState = m_buttonUpActs[i].Act(args, gameObject, this);

            if (ControllableStateMachine.remainState != m_returnState && ControllableStateMachine.previousState != m_returnState)
            {
                continue;
            }

            if (ControllableStateMachine.remainState != m_tempState && m_tempState != m_returnState)
            {
                m_returnState = m_tempState;
            }
        }

        return m_returnState;
    }

    /// <summary>
    /// 按钮按住的函数
    /// </summary>
    /// <param name="args"></param>
    public virtual ControllableObjectState ButtonPressed(ButtonEventArgs args, GameObject gameObject)
    {
        m_returnState = ControllableStateMachine.remainState;

        for (int i = 0, length = m_buttonPressedActs.Length; i < length; i++)
        {
            m_tempState = m_buttonPressedActs[i].Act(args, gameObject, this);

            if (ControllableStateMachine.remainState != m_returnState && ControllableStateMachine.previousState != m_returnState)
            {
                continue;
            }

            if (ControllableStateMachine.remainState != m_tempState && m_tempState != m_returnState)
            {
                m_returnState = m_tempState;
            }
        }

        return m_returnState;
    }

    public override void EnterState(GameObject gameObject)
    {
        isAtTop = true;

        isNeedToPop = false;
    }

    public override void ExitState(GameObject gameObject)
    {
        isAtTop = false;

        isNeedToPop = true;
    }

    public override void StateUpdate(GameObject gameObject)
    {

    }
}
